package com.jp.knowledge.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.jp.knowledge.R;
import com.jp.knowledge.model.DailyKnowledgePackageModel;
import com.jp.knowledge.model.KnowlageListModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;

import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/6/29.
 */

public class  AlbumImgAdapter extends RecycleVAdapter<DailyKnowledgePackageModel.ListBean> implements View.OnClickListener {

	public AlbumImgAdapter(Context context, @NonNull List list) {
		super(context, list);
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.fragment_collection_ui;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		DailyKnowledgePackageModel.ListBean data = getData(position);

		holder.setTextView(R.id.ui_title, data.getName());
		ImageView collectionIcon = holder.getImageView(R.id.ui_collection);
		collectionIcon.setImageResource(data.getIsFollow() == 1 ? R.mipmap.kuaitiyanyishoucang : R.mipmap.kuaitiyanyishoucanghuise);
		collectionIcon.setTag(position);
		collectionIcon.setOnClickListener(this);
		collectionIcon.setVisibility(data.getLayout() == 5 ? View.GONE : View.VISIBLE);
		holder.getTextView(R.id.ui_title).setGravity(data.getLayout() == 5 ? Gravity.CENTER : Gravity.START);

		final FrameLayout frameLayout = holder.get(R.id.ui_img_frame);
		final ImageView cover = holder.getImageView(R.id.ui_img);
		cover.setScaleType(ImageView.ScaleType.CENTER);
		if(data.getLayout() == 5){
			cover.setImageResource(R.mipmap.album_folder);
		} else{
			cover.setImageResource(R.mipmap.morentutouxiang);
			Glide.with(this.mContext)
					.load(data.getCover()+"?imageMogr2/format/jpg/quality/75/thumbnail/!100p")
					.asBitmap()
					.diskCacheStrategy(DiskCacheStrategy.SOURCE)
					.into(new ViewTarget<View, Bitmap>(cover){
						@Override
						public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
							if(resource != null && !resource.isRecycled()) {
								int itemWidth = frameLayout.getMeasuredWidth();
								cover.getLayoutParams().width = itemWidth;
								cover.getLayoutParams().height = itemWidth * resource.getHeight() / resource.getWidth();
								cover.setScaleType(ImageView.ScaleType.CENTER_CROP);
								cover.setImageBitmap(resource);
							}
						}
					});
		}
	}

	@Override
	public void onClick(View v) {
		if(itemCollectionListener != null)
			itemCollectionListener.onItemCollectionClick((int) v.getTag());
	}

	private ItemCollectionListener itemCollectionListener;
	public void setItemCollectionListener(ItemCollectionListener itemCollectionListener){
		this.itemCollectionListener = itemCollectionListener;
	}
	public interface ItemCollectionListener{
		void onItemCollectionClick(int position);
	}
}
